using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
//using System.Drawing;
using System.Diagnostics;
using System.Xml;
using System.Web;
using Mail = System.Web.Mail;
using System.Web.Services;
using System.IO;
using System.Text.RegularExpressions;
using Dimok;
using ConfigWS = Dimok.Config.imaging.WS;
using Dimok.Data;
using Dimok.Exceptions;
using Dimok.Data.Database;
using Dimok.Images;
using Tiffer;

////////////////////////////////////////////////////////////////////////////////////////////////////
// namespace: Dimok.Images.WS
//
// summary:	.
////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Dimok.Images.WS {

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Imaging. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class Imaging : System.Web.Services.WebService {
		#region ============== Basics ================
		protected UserObjectEx uox = new UserObjectEx();
		private HttpResponse Response;

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Default constructor. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public Imaging() {
			//CODEGEN: This call is required by the ASP.NET Web Services Designer
			InitializeComponent();
			Response = Context.Response;
		}
		#endregion ====================================

		#region Component Designer generated code
		
		//Required by the Web Services Designer 
		private IContainer components = null;

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	
		/// Required method for Designer support - do not modify the contents of this method with the code
		/// editor. 
		/// </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private void InitializeComponent() {
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Clean up any resources being used. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="disposing">	true if resources should be disposed, false if not. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		protected override void Dispose( bool disposing ) {
			if(disposing && components != null) {
				components.Dispose();
			}
			base.Dispose(disposing);		
		}
		
		#endregion

		#region ==================== Moving ====================

		#region MoveFolderToFolder

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Move folder to folder. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="applID">						Identifier for the appl. </param>
		/// <param name="fileOrFolderFrom">	The file or folder from. </param>
		/// <param name="pageNumbers">			The page numbers. </param>
		/// <param name="fileOrFolderTo">		The file or folder to. </param>
		/// <param name="pageAfter">				The page after. </param>
		/// <param name="copy">							true to copy. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod(EnableSession=true)]
		public string[] MoveFolderToFolder(int applID,string fileOrFolderFrom,int[] pageNumbers,string fileOrFolderTo,int pageAfter,bool copy){
			try{
				if( Common.IsNumeric(fileOrFolderFrom) ){
					int folderFrom = int.Parse(fileOrFolderFrom);
					if( Common.IsNumeric(fileOrFolderTo) ){
						int folderTo = int.Parse(fileOrFolderTo);
						uox.ImagePagesMove(applID,folderFrom,new ArrayList(pageNumbers).ToArray(),folderTo,pageAfter);
						return new string[]{GetPagesList(applID,folderFrom),GetPagesList(applID,folderTo)};
					}else return MoveFolderToFile(applID,folderFrom,pageNumbers,fileOrFolderTo,pageAfter,copy);
				}else if( Common.IsNumeric(fileOrFolderTo) ){
					int folderTo = int.Parse(fileOrFolderTo);
					return MoveFileToFolder(applID,fileOrFolderFrom,pageNumbers,folderTo,pageAfter,copy);
				}else return MoveFileToFile(fileOrFolderFrom,pageNumbers,fileOrFolderTo,pageAfter,copy);
			}catch(Exception exc){throw new ClientException(exc);}
		}
		#endregion MoveFolderToFolder

		#region MoveFolderToFile

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Move folder to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="applID">				Identifier for the appl. </param>
		/// <param name="folderID">			Identifier for the folder. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		/// <param name="fileName">			Filename of the file. </param>
		/// <param name="pageAfter">		The page after. </param>
		/// <param name="copy">					true to copy. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string[] MoveFolderToFile(int applID,int folderID,int[] pageNumbers,string fileName,int pageAfter,bool copy) {
			try {
				object[] pages = new ArrayList(pageNumbers).ToArray();
				byte[] image = null;
				try{
					fileName = ImageFiles.FilePathFull(fileName);
					image = uox.ImageFolder(applID,folderID,Enums.GetPageType(fileName),pages);
				}catch(EmptyFileException){
					image = uox.ImageFolder(applID,folderID,pages);
					fileName = ImageFiles.FilePathFull(fileName,Enums.GetExtention(image));
				}
				if( pageAfter < 0 )
					pageAfter = File.Exists(fileName) ? ImageFiles.PageCount(fileName) : 0;
				ImageFiles.AddPages(image,fileName,pageAfter);
				if( !copy )uox.ImagePagesDelete(applID,folderID,pages);
				return new string[]{GetPagesList(applID,folderID),GetPagesList(fileName)};
			}
			catch( Exception exp ){throw new ClientException(exp);}
		}
		#endregion MoveFolderToFile

		#region MoveFileToFile

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Move file to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="fileFrom">			The file from. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		/// <param name="fileTo">				The file to. </param>
		/// <param name="pageAfter">		The page after. </param>
		/// <param name="copy">					true to copy. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string[] MoveFileToFile(string fileFrom,int[] pageNumbers,string fileTo, int pageAfter,bool copy) {
			try {
				fileFrom = ImageFiles.FilePathFull(fileFrom);
				fileTo = ImageFiles.FilePathFull(fileTo,Enums.GetExtention(fileFrom));
				if( Enums.GetPageType(fileTo) != Enums.GetPageType(fileFrom) )
					throw new ClientException("Moving/Coping between files of different types is not supported.");
				if( pageAfter < 0 )
					pageAfter = File.Exists(fileTo) ? ImageFiles.PageCount(fileTo) : 0;
				ImageFiles.MovePages(fileFrom,fileTo,pageAfter,copy,pageNumbers);
				return new string[] {GetPagesList(fileFrom),GetPagesList(fileTo)};
			}
			catch( Exception exp ){throw new ClientException(exp);}
		}
		#endregion MoveFileToFile

		#region MoveFileToFolder

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Move file to folder. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="applID">				Identifier for the appl. </param>
		/// <param name="fileFrom">			The file from. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		/// <param name="folderTo">			The folder to. </param>
		/// <param name="pageAfter">		The page after. </param>
		/// <param name="copy">					true to copy. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string[] MoveFileToFolder(int applID,string fileFrom,int[] pageNumbers,int folderTo,int pageAfter,bool copy) {
			byte[] filePages = ImageFiles.GetPages(fileFrom,pageNumbers);
			uox.ImagePagesInsert(applID,folderTo,filePages,pageAfter);
			if( !copy )ImageFiles.DeletePages(fileFrom,pageNumbers);
			return new string[]{GetPagesList(fileFrom),GetPagesList(applID,folderTo)};
		}
		#endregion

		#endregion

		#region =================== Deleting =====================

			#region DeleteFile

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Deletes the file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="fileName">			Filename of the file. </param>
		/// <param name="pagesNumbers">	The pages numbers. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string DeleteFile(string fileName,int[] pagesNumbers) {
			try {
				ImageFiles.DeletePages(fileName,pagesNumbers);
				return GetPagesList(fileName);
			}
			catch( Exception exp ){throw new ClientException(exp);}
		}
			#endregion DeleteFilePage

			#region DeleteApplPage

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Deletes the appl page. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="applID">				Identifier for the appl. </param>
		/// <param name="folderID">			Identifier for the folder. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string DeleteApplPage(int applID,int folderID,int[] pageNumbers) {
			try {
				uox.ImagePagesDelete(applID,folderID,new ArrayList(pageNumbers).ToArray());
				return GetPagesList(applID,folderID);
			}catch( Exception exp ){throw new ClientException(exp);}
		}
			#endregion
		
		#endregion

		#region ================== Lists ==================

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the pages list. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ImagingException">	Thrown when imaging. </exception>
		///
		/// <param name="fileName">	Filename of the file. </param>
		///
		/// <returns>	The pages list. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public string GetPagesList(string fileName){
			try {
				ImagePageDS.PageDataTable dtPages = new Dimok.Images.ImagePageDS().Page; 
				int pc = ImageFiles.PageCount(fileName);
				while( pc-- > 0 )
					dtPages.AddPageRow(Path.GetFileName(fileName),Path.GetFileNameWithoutExtension(fileName));
				return SQLHelperEx.XmlWithSchema(dtPages);
			}catch(Exception exc){throw new ImagingException("\nFileName:"+fileName,exc);}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the pages list. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="applID">		Identifier for the appl. </param>
		/// <param name="folderID">	Identifier for the folder. </param>
		///
		/// <returns>	The pages list. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public string GetPagesList(int applID,int folderID){return GetPagesList(applID+"",folderID);}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the pages list. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="fileNameOrApplID">	Identifier for the file name or appl. </param>
		/// <param name="folderID">					Identifier for the folder. </param>
		///
		/// <returns>	The pages list. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string GetPagesList(string fileNameOrApplID,int folderID){
			try{
				if( !Common.IsNumeric(fileNameOrApplID) )return GetPagesList(fileNameOrApplID);
				return SQLHelperEx.XmlWithSchema(uox.db_Images,"SELECT * FROM IE_PagesList(@ApplID,@FolderID) ORDER BY PageNumber",fileNameOrApplID,folderID);
			}catch( Exception exp ){throw new ClientException(exp);}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets an image folders list. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="applID">	Identifier for the appl. </param>
		///
		/// <returns>	The image folders list. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string GetImageFoldersList(int applID){
			try{
				return SQLHelperEx.XmlInnerText(Pages.ImageList(uox.db_Images,applID));
			}catch(Exception exc){throw new ClientException(exc);}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the files list. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <returns>	The files list. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string GetFilesList(){
			try{
				return SQLHelperEx.XmlInnerText(uox.menuIDParam==0?List.FilesList():List.FilesList(uox.menuIDParam));
			}catch(Exception exc){throw new ClientException(exc);}
		}
		#endregion

		#region =================== New =====================

			#region NewFileFromFolder

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Creates a new file from folder. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ApplicationException">	Thrown when application. </exception>
		/// <exception cref="ClientException">			Thrown when client. </exception>
		///
		/// <param name="applID">				Identifier for the appl. </param>
		/// <param name="newFile">			The new file. </param>
		/// <param name="folderID">			Identifier for the folder. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		/// <param name="copy">					true to copy. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public int NewFileFromFolder(int applID,string newFile,int folderID,int[] pageNumbers,bool copy) {
			try{
				object[] pages = new ArrayList(pageNumbers).ToArray();
				byte[] image = uox.ImageFolder(applID,folderID,pages);
				if( !Path.HasExtension(newFile) )newFile += '.'+Enums.GetExtention(image);
				newFile = ImageFiles.FilePathFull(newFile);
				if( File.Exists(newFile) )throw new ApplicationException("File "+Path.GetFileNameWithoutExtension(newFile)+" allready exists.");
				if( image.Length == 0 )throw new ApplicationException("\n\nCan't create file from empty folder.\n\n");
				using(FileStream fs = File.OpenWrite(newFile))
					fs.Write(image,0,image.Length);
				if( !copy )
					uox.ImagePagesDelete(applID,folderID,pages);
				return uox.ImagePageCount(applID,folderID);
			}catch(Exception exc){throw new ClientException(exc);}
		}
			#endregion

			#region NewFileFromFile

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Creates a new file from file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="newFile">		The new file. </param>
		/// <param name="fileName">		Filename of the file. </param>
		/// <param name="pageNum">		The page number. </param>
		/// <param name="pageCount">	Number of pages. </param>
		/// <param name="copy">				true to copy. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public int NewFileFromFile(string newFile,string fileName,int pageNum,int pageCount,bool copy) {
			try{
				fileName = ImageFiles.FilePathFull(fileName);
				/*
				if( !Path.HasExtension(newFile) )newFile += "." + Enums.GetExtention(fileName);
				newFile = ImageFiles.FilePathFull(newFile);
				if( pageNum <= 0 && pageCount <= 0 ){
					if( !copy )File.Move(fileName,newFile);
					else File.Copy(fileName,newFile);
					return 0;
				}
				if( File.Exists(newFile) )throw new ApplicationException("File "+newFile+" allready exists.");
				byte[] image = ImageFiles.GetPages(fileName,pageNum,pageCount);
				using(FileStream fs = new FileStream(newFile,FileMode.Create))
					fs.Write(image,0,image.Length);
				if( !copy )return DeleteFilePage(fileName,pageNum,pageCount);
				*/
				return ImageFiles.PageCount(fileName);
			}catch(Exception exc){throw new ClientException(exc);}
		}
			#endregion
		
		#endregion
		
		#region GetApplPath

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets an appl path. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="applID">	Identifier for the appl. </param>
		///
		/// <returns>	The appl path. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public virtual string[] GetApplPath(int applID){
			try{
				uox.applID = applID;
				return new string[]{uox.ApplInfo.MenuPath(),Pages.ImageList(uox.db_Images,applID).GetXml()};
			}catch(Exception exc){throw new ClientException(exc);}
		}
		#endregion

		#region Applicant

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Adds an applicant. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="fileName">			Filename of the file. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		/// <param name="folderID">			Identifier for the folder. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string AddApplicant(string fileName,int[] pageNumbers,string folderID){
			try{
				int applID = uox.AddApplicant(fileName);
				if( Common.IsNumeric(fileName) )
					SQLHelperEx.ExecuteNonQueryEx(uox.db_Images,"s_MovePages_Ex",fileName,folderID,new StringList(pageNumbers).Join(),1,0,applID);
				else MoveFileToFolder(applID,fileName,pageNumbers,1,0,false);
				return GetPagesList(fileName,0);
			}catch( Exception exp ){throw new ClientException(exp);}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Searches for the first by usix or vin. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="filter">	The filter. </param>
		///
		/// <returns>	The found by usix or vin. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string FindByUsixOrVin(string filter) {
			try{
				return SQLHelperEx.ExecuteDatasetEx(uox.db, "s_FindByUsixOrVin", filter).GetXml();
			} catch (Exception exp) { throw new ClientException(exp); }
		}
		#endregion Applicant

		#region =================== Mail ===================

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Mail folder. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="to">						to. </param>
		/// <param name="subject">			The subject. </param>
		/// <param name="applID">				Identifier for the appl. </param>
		/// <param name="folderID">			Identifier for the folder. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void MailFolder(string to,string subject,int applID,int folderID,int[] pageNumbers) {
			try{
				byte[] image = uox.ImageFolder(applID,folderID,new ArrayList(pageNumbers).ToArray());
				string fileTemp = FileManager.GetFileFromBytes(image,"."+Enums.GetExtention(image));

				MailFile(to,subject,fileTemp,-1);
				File.Delete(fileTemp);
			}catch( Exception exp ){throw new ClientException(exp);}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Mail file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="to">						to. </param>
		/// <param name="subject">			The subject. </param>
		/// <param name="fileName">			Filename of the file. </param>
		/// <param name="pageNumbers">	The page numbers. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void MailFile(string to,string subject,string fileName,params int[] pageNumbers) {
			try {
				string fileTemp = "";
				fileName = ImageFiles.FilePathFull(fileName);
				Mail.MailMessage ml = new Mail.MailMessage();
				ml.To = to;
				if( subject != null && subject != "" )ml.Subject = subject;
				ml.From = uox.Mail;
				if( !FileManager.IsAllPages(pageNumbers) ) {
					byte[] file = ImageFiles.GetPages(fileName,pageNumbers);
					fileTemp = FileManager.BuildFullFileName("."+Enums.GetExtention(file),true);
					FileStream fs = File.Create(fileTemp);
					fs.Write(file,0,file.Length);
					fs.Close();
					fileName = fileTemp;
				}
				ml.Attachments.Add( new Mail.MailAttachment(fileName) );
				Mail.SmtpMail.SmtpServer = Dimok.Config.application.MailServer;
				Mail.SmtpMail.Send(ml);
				if( fileTemp != "" )File.Delete(fileTemp);
			}
			catch(Exception exp) {
				//----------------------
				throw new ClientException(exp,"File",fileName);
			}//-------------------------------------------
		}
		#endregion Mail

		#region ========== Images ===========

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Page history. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="applID">				Identifier for the appl. </param>
		/// <param name="folderTypeID">	Identifier for the folder type. </param>
		/// <param name="pageNumber">		The page number. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string PageHistory(int applID,int folderTypeID,int pageNumber) {
			try {
				return (string)SQLHelperEx.ExecuteScalarEx(uox.db_Images,
					@"SELECT UserName + ',' + CONVERT(varchar,DateStamp) AS HistInfo FROM  vhist_t_Page
								WHERE (ApplicantID = @ApplID) AND (ImageTypeID = @ImageTypeID) AND (PageNumber = @PageNumber)",applID,folderTypeID,pageNumber);
			}
			catch( Exception exp ){throw new ClientException(exp);}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Page count appl. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="applID">		Identifier for the appl. </param>
		/// <param name="folderID">	Identifier for the folder. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public int PageCount_Appl(int applID,int folderID){
			try{
				return uox.ImagePageCount(applID,folderID);
			}catch( Exception exp ){throw new ClientException(exp);}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Page count. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="fileName">	Filename of the file. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public int PageCount(string fileName) {
			try {
				//----------------------------------------
				fileName = ImageFiles.FilePathFull(fileName);
				if( !File.Exists(fileName) )return 0;
				return ImageFiles.PageCount(fileName);
			}catch(Exception exc){throw new ClientException("\nFileName:"+fileName,exc,new NameValueCollection());}
		}

			#region Folders

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Folder update. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="xmlDS">	Element describing the xml ds. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public int Folder_Update(XmlElement xmlDS){
			try{
				string sql = "SELECT * FROM t_ImageType WHERE ";
				DataSet dsFolder = SQLHelperEx.LoadDataSet(xmlDS);
				int id = int.Parse(dsFolder.Tables[0].Rows[0]["ID"].ToString());
				SQLHelperEx.UpdateDataSet(dsFolder,uox.db_Images,sql + "ID=@ID",id);
				if( id == 0 )
					id = (int)SQLHelperEx.ExecuteScalarEx(uox.db_Images,sql+"ImageType=@IT",dsFolder.Tables[0].Rows[0]["ImageType"]);
				return id;
			}catch( Exception exp ){throw new ClientException(exp);}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Folder delete. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="id">	The identifier. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void Folder_Delete(int id){
			try{
				if( (int)SQLHelperEx.ExecuteScalarEx(uox.db_Images,"SELECT COUNT(*)FROM t_Image WHERE(ImageTypeID=@ID)",id) > 0 )
					throw new ClientException("Some Applicants have image in this folder.\n\nMove them some where else and try again");
				SQLHelperEx.ExecuteNonQueryEx(uox.db_Images,"DELETE FROM t_ImageType WHERE ID=@ID",id);
			}catch( Exception exp ){throw new ClientException(exp);}
		}
			#endregion Folders

		#endregion ===========================

		#region ========= Privates ==========
		#endregion==========================
	}
}